
$(document).ready(function() {

    //图片上传地址
    var upload_url = "http://101.52.128.253:8080/imgServer/mongo/upload.do";

    //上传方法
    var upload = function(f,clientId) {

        var formData = new FormData();

        formData.append('file', f);
        formData.append('clientId', clientId);

        var xhr = new createXmlHttpRequest();

        xhr.open('POST', upload_url, true);
        xhr.send(formData);

        //发送请求完成后执行该方法
        xhr.onreadystatechange = function (){

            if(xhr.readyState == 4 && xhr.status == 200){  

                var data = xhr.responseText;    

                smmImageUploadCallBack(JSON.parse(data));
            }    

        }; 
    }


    //上传按钮点击事件
    $("#smm-image-upload-btn").click(function() {


        var clientid=$("#smm-image-upload-clientId").val();

        if(clientid=="") {

            var data={"code":"000001","data":"clientid 不能为空"};

            smmImageUploadCallBack(data);

            return ;
        }

        //定义文件最大字节数。
        var maxFileSize=5*1024*1024;

        //获取文件对象
        var file = $("#smm-image-upload-file")[0].files[0];

        //检查文件大小
        if(file.size>maxFileSize){

            var data={"code":"000001","data":"上传文件大小不能超过 "+ ((maxFileSize/1024)/1024).toFixed(2) +" MB"};

            smmImageUploadCallBack(data);

            return ;
        }

        upload(file,clientid);

    })
})


//XmlHttpRequest对象    
function createXmlHttpRequest(){    
    if(window.ActiveXObject){ //如果是IE浏览器    
        return new ActiveXObject("Microsoft.XMLHTTP");    
    }else if(window.XMLHttpRequest){ //非IE浏览器    
        return new XMLHttpRequest();    
    }    
}   

